package com.wbsy.officechat;

import java.util.ArrayList;

import com.wbsy.officechat.app.OfficeApplication;
import com.wbsy.officechat.service.ListeneService;
import com.wbsy.officechat.util.CheckNet;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

/**
 * 
 * @author wangzhiqiang @
 */
public class StartActivity extends Activity {

	private TextView animText; // 要移动的文字
	private AlertDialog.Builder builder = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		animText = (TextView) StartActivity.this.findViewById(R.id.anim_text);

		// 动画
		AnimationSet animationSet = new AnimationSet(true);
		TranslateAnimation myAnim = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
				Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
				-0.3f);
		myAnim.setDuration(2000);
		animationSet.setStartOffset(800);
		animationSet.addAnimation(myAnim);
		animationSet.setFillAfter(true);

		if (new CheckNet(StartActivity.this).checkNetConn()) {
			// if(true){
			animText.startAnimation(animationSet);
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						Thread.sleep(4000);
						startActivity(new Intent(StartActivity.this,
								MainActivityNew.class));
						if (!StartActivity.this.isWorked()) {
							startService(new Intent(StartActivity.this,
									ListeneService.class));
						}
						StartActivity.this.finish();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}).start();
		} else {
			showAlert();
		}
		OfficeApplication.getInstance().addActivity(this);
	}

	public void showAlert() {
		builder = new AlertDialog.Builder(StartActivity.this);
		builder.setTitle("OfficeChat温馨提醒").setMessage("仅限在wifi环境下使用，请检查网络设置");
		builder.setPositiveButton("设置网络",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						startActivity(new Intent(
								android.provider.Settings.ACTION_SETTINGS));
						dialog.cancel();
						StartActivity.this.finish();

					}
				});
		builder.setCancelable(false);
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				StartActivity.this.finish();
			}

		});
		builder.show();
	}

	public boolean isWorked() {
		ActivityManager myManager = (ActivityManager) StartActivity.this
				.getSystemService(Context.ACTIVITY_SERVICE);
		ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager
				.getRunningServices(30);
		for (int i = 0; i < runningService.size(); i++) {
			if (runningService.get(i).service.getClassName().toString()
					.equals("com.wbsy.officechat.service.ListeneService")) {
				return true;
			}
		}
		return false;
	}

}
